<template>
	<view class="wrap">
		<form  @submit="submit">
			<view class="top">
				<view class="item">
					<view class="left">收货人</view>
					<input type="text" 
						placeholder-class="line" 
						placeholder="请填写收货人姓名" 
						name="username"
						v-model="username"
						maxlength="10"
					 />
				</view>
				<view class="item">
					<view class="left">手机号码</view>
					<input type="text" 
						placeholder-class="line"  
						placeholder="请填写收货人手机号" 
						name="userphone"
						v-model="userphone"
						maxlength="11"
					/>
				</view>
				
				<view class="item" @click="regionVisible=true">
					<view class="left">所在地址</view>
					<input
						disabled
						type="text"
					  	placeholder-class="line" 
					  	placeholder="省市区县、乡镇等" 
					  	name="location" 
					  	v-model="location"
						maxlength="20"
					  />
				</view>
				
				<view class="item address">
					<view class="left">详细地址</view>
					<textarea type="text" 
						placeholder-class="line" 
						placeholder="街道、楼牌等" 
						name="useraddress"
						v-model="useraddress"
						maxlength="50"
					/>
				</view>
				<!-- <view class="site-clipboard">
					<textarea placeholder-class="line" value="" placeholder="粘贴文本,可自动识别姓名和地址等" />
					<view class="clipboard">
						地址粘贴板
						<u-icon name="arrow-down" class="icon" :size="20"></u-icon>
					</view>
				</view> -->
			</view>
			<view class="bottom">
				<view class="tag">
					<view class="left">标签</view>
					<view class="right">
						<u-radio-group v-model="label" @change="radioGroupChange" active-color="orange">
							<u-radio 
								v-for="(item, index) in list" :key="index" 
								:name="item.name"
							>
								{{item.name}}
							</u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="default">
					<view class="left">
						<view class="set">设置默认地址</view>
						<view class="tips">提醒：每次下单会默认推荐该地址</view>
					</view>
					<view class="right">
						<switch 
							color="red" 
							@change="setDefault" 
							name="status"
							:checked="status"
						 />
					</view>
				</view>
			</view>
			<regionPicker
				:visible.sync="regionVisible"
				mode="region"
				default-type="value"
				:hide-area="false"
				@confirm="onConfirm($event,'region')"
				ref="region" 
			></regionPicker>
			<button form-type="submit" type="warn">保存并使用</button>
			<button form-type="reset" >重置</button>
		</form>
	</view>
</template>

<script>

// 引入提示模块 
let {load,hideload,isLogin,tip} = require('@/utils/tip.js');
let app = getApp();
import regionPicker from "@/components/region/w-picker.vue";

export default {
	components:{regionPicker},
	
	data() {
		return {
			regionVisible:false,//初始化地区选择器开关	
			username:'',//初始化收件人姓名
			userphone:'',//初始化收件人手机号
			useraddress:'',//初始化收件人详情地址
			location:'山东省滨州市无棣县',//初始化地区默认值
			label:'家',//初始化地址标签
			list: [
				{
					name: '家'
				},
				{
					name: '公司'
				},
				{
					name: '学校'
				}
			],
			status:false,
			siteUrl:'addressadd',//初始化请求url地址
			id:'',//初始化修改数据id
		};
	},
	onLoad(option){
		
		this.edit()
	},
	methods: {
		//数据回显
		async edit(){
			let id=this.$mp.query.id
			if(!id){
				return
			}
			
			this.id=id
			
			// 修改导航标题
			uni.setNavigationBarTitle({
				title:"修改地址"
			})
			
			// 请求当前的地址信息
			
			let {uid,token}=uni.getStorageSync("userinfo")
			let [err,res]=await uni.request({
				url:this.$baseUrl+"/api/addresslist",
				header:{
					 authorization:token
				},
				data:{
					uid,
					id
				}
			})
			// console.log(res);
			if(err){
				 uni.showToast({
						title:"出错了",
						icon:"error"
					 })
			 return
			}
			if(res.data.code==403){
					uni.showToast({
					title:"登录过期",
					icon:"error"
				 })
			 setTimeout(()=>{
				 uni.navigateTo({
						 url:"/pages/login/login"
					  })
								 						 
				 },1000)
								 
				 return
								 
			}
			if(res.data.code !=200){
				 uni.showToast({
						title:"出错了",
						icon:"error"
					 })
								 
				 return
			}
			
			this.username=res.data.list[0].username
			this.userphone=res.data.list[0].userphone
			this.location=res.data.list[0].location
			this.useraddress=res.data.list[0].useraddress
			this.ladel=res.data.list[0].ladel
			this.status=res.data.list[0].status?true:false
			
			
		},
		async submit(e){
			
			 let {uid,token}=uni.getStorageSync("userinfo")
			// let data2={
			// 	  ...e.detail.value,
			// 		status:e.detail.value.status?1:0,
			// 		 ladel:this.label,
			// 		 id:this.id
					 
			// }
			// let data1={
			// 	 ...e.detail.value,
			// 	status:e.detail.value.status?1:0,
			// 	 ladel:this.label,
			// 	 uid
			// }
			
			let data ={
				...e.detail.value,
				status:e.detail.value.status?1:0,
				label:this.label,
				id:this.id,
				uid
			}
			
			
			let [err,res]=[]
			if(this.id){
				// 修改
				[err,res]=await uni.request({
					url:this.$baseUrl+"/api/addressedit",
							header:{
								 authorization:token
							},
							data,
							method:"POST"
				})
				
			}else{
				// 新增
				 [err,res]=await uni.request({
					url:this.$baseUrl+"/api/addressadd",
							header:{
								 authorization:token
							},
							data,
							method:"POST"
				})
				
			}
		   
		   if(err){
		   	 uni.showToast({
		   			title:"出错了",
		   			icon:"error"
		   		 })
		    return
		   }
		   if(res.data.code==403){
		   		uni.showToast({
		   		title:"登录过期",
		   		icon:"error"
		   	 })
		    setTimeout(()=>{
		   	 uni.navigateTo({
		   			 url:"/pages/login/login"
		   		  })
		   					 						 
		   	 },1000)
		   					 
		   	 return
		   					 
		   }
		   if(res.data.code !=200){
		   	 uni.showToast({
		   			title:"出错了",
		   			icon:"error"
		   		 })
		   					 
		   	 return
		   }
		   
		   uni.showToast({
		   	title:`地址${this.id?'修改':'新增'}`
		   })
		   setTimeout(()=>{
			   uni.navigateBack()
		   },1000)
		   
		},
		// 提交收货地址 http://localhost:3000/api/addressadd
		// async submit(e){
		//    let {uid,token}=uni.getStorageSync("userinfo")
		   
		//    let data={
		// 	   ...e.detail.value,
		// 	   status:e.detail.value.status?1:0,
		// 	   ladel:this.label,
		// 	   uid
		//    }
		   
		//    let [err,res]=await uni.request({
		//    	url:this.$baseUrl+"/api/addressadd",
		// 	header:{
		// 		 authorization:token
		// 	},
		// 	data,
		// 	method:"POST"
		//    })
		   
		   
		//    if(err){
		//    	 uni.showToast({
		//    			title:"出错了",
		//    			icon:"error"
		//    		 })
		//     return
		//    }
		//    if(res.data.code==403){
		//    		uni.showToast({
		//    		title:"登录过期",
		//    		icon:"error"
		//    	 })
		//     setTimeout(()=>{
		//    	 uni.navigateTo({
		//    			 url:"/pages/login/login"
		//    		  })
		   					 						 
		//    	 },1000)
		   					 
		//    	 return
		   					 
		//    }
		//    if(res.data.code !=200){
		//    	 uni.showToast({
		//    			title:"出错了",
		//    			icon:"error"
		//    		 })
		   					 
		//    	 return
		//    }
		   
		//    uni.showToast({
		//    	title:"地址添加成功"
		//    })
		//    setTimeout(()=>{
		// 	   uni.navigateBack()
		//    },1000)
		   
		// },
		
		
		// 设置会员地区
		onConfirm(res){
			this.location = res.result;
		},
		
		// 设置默认地址
		setDefault(){},
		
		// 设置底子标签
		radioGroupChange(){}
		
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/addsite.scss';
</style>
